﻿import socket

down=set()
todown=set()
todown.add("http://blog.sina.com.cn/")
server="166.111.180.201"
port=1088
TOTAL=1000000

			
def send(s):
	count=0
	tmp=[]
	for url in todown:
		tmp.append(url)
		down.add(url)
		count=count+1
		s.send(url+",")
		print url
		if count>10:
			break
	s.send("!")
	for url in tmp:
		todown.remove(url)
	return count
	

s=socket.socket()
s.bind((server,port))
s.listen(20)
gcount=0
while True:
	client,cadd=s.accept()
	print "connent with:"+str(cadd)+"\n"
	data=client.recv(512)
	if data=="get":
		if gcount>TOTAL:
			client.send("*")
			break
		tmp=send(client)
		gcount=gcount+tmp
		print "send "+str(cadd)+" "+str(tmp)+"~~~~~~with ",len(todown)," left~~~~~totally has downloaded ",len(down)," urls"
	else:
		url=""
		client.send("OK")
		print "Start receive from"+str(cadd)+"\n"
		while True:
			tmp=client.recv(4096)
			if tmp.find("!")>=0:#finished
				tmp=tmp.replace("!","")	
				url=url+tmp	
				break
			url=url+tmp
		url=url.split(",")
		tmp=0
		
		if(len(down)+len(todown)<=TOTAL)
			for u in url:
				if u not in down:
					print u
					todown.add(u)
					tmp=tmp+1
			print "receive from "+str(cadd)+" "+str(tmp)+" urls\n"
		else:
			print "receive from "+str(cadd)+" "+len(url)+" urls, but we have enough\n"
pf=open("urls","w")
pf.write("down\n")
for url in down:
	pf.write(url+"\n")
	
pf.write("To be down\n")
for url in todown:
	pf.write(url+"\n")
s.close()

